﻿using IOCDI.IBLL;
using IOCDI.IDAL;
using IOCDI.Model;

namespace IOCDI.BLLV2
{
    public class UserBLLV2 : IUserBLL
    {
        //如果这里依赖细节:UserDAL、UserDALMySql,那么每次都需要修改BLL层代码,假设加一个UserDALOracle就有需要改代码
        //换成抽象IUserDAL,通过构造函数注入,而不是new,则不会有这方面问题,切换细节时,只需要修改注入配置即可
        //DIP的好处:
        //1.依赖细节,会导致低层的变化影响高层.依赖抽象低层的变化就不会影响高层,保持稳定,可扩展
        //2.一套代码去满足不同类型的需求

        //private IUserDAL _iUserDAL = new UserDAL();
        private IUserDAL _iUserDAL = null;
        public UserBLLV2(IUserDAL userDAL)
        {
            this._iUserDAL = userDAL;
        }

        //private UserDAL _iUserDAL = null;
        //public UserBLL(UserDAL userDAL)
        //{
        //    this._iUserDAL = userDAL;
        //}

        //private UserDALMySql _iUserDAL = null;
        //public UserBLL(UserDALMySql userDAL)
        //{
        //    this._iUserDAL = userDAL;
        //}

        public void LastLogin(UserModel user)
        {
            user.LoginTime = DateTime.Now;
            this._iUserDAL.Update(user);
        }

        public UserModel Login(string account)
        {
            return this._iUserDAL.Find(u => u.Account.Equals(account));
        }
    }
}